package com.heima.behavior.listener;

import com.alibaba.fastjson.JSON;
import com.heima.behavior.service.IApFollowBehaviorService;
import com.heima.user.dto.FollowBehaviorDto;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

/**
 * @author: itheima
 * @create: 2022-07-06 16:05
 */
@Slf4j
@Component
public class FollowBehaviorListener {


    @Autowired
    private IApFollowBehaviorService followBehaviorService;

    /**
     * 监听用户关注 行为记录
     *
     * @param record 关注行为消息
     */
    @KafkaListener(topics = "${topic.followBehaviorTopic}")
    public void handlerMsg(ConsumerRecord<String, String> record) {
        if (StringUtils.isNotBlank(record.value())) {
            log.info("【行为微服务】监听到关注作者行为：{}", record.value());
            FollowBehaviorDto dto = JSON.parseObject(record.value(), FollowBehaviorDto.class);
            followBehaviorService.saveFollowBehavior(dto);
        }

    }

}
